package aobing;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

public class ThreadFactoryTest implements ThreadFactory {
    @Override
    public Thread newThread(Runnable r) {
        Thread t = new Thread(r);
        t.setDaemon(true);
        return t;
    }

    static class DaemonFromFactory implements Runnable {

        @Override
        public void run() {
            while (true) {
                try {
                    TimeUnit.MILLISECONDS.sleep(100);
                    System.out.println(Thread.currentThread() + " " + this);
                } catch (InterruptedException e) {
                    System.out.println("Interrupted");
                }
            }
        }

        public static void main(String[] args) throws InterruptedException {
            ExecutorService service = Executors.newCachedThreadPool(new ThreadFactoryTest());
            for (int i = 0; i < 10; i++) {
                service.execute(new DaemonFromFactory());
            }
            System.out.println("All daemons started");
            TimeUnit.MILLISECONDS.sleep(500);
            service.shutdown();
        }
    }
}
